{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import numpy\n",
    "from deap import base,creator, tools,algorithms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Types\n",
    "creator.create(\"Fitness\", base.Fitness, weights=(-1.0,1.0))\n",
    "# weights 1.0, 求最大值,-1.0 求最小值\n",
    "creator.create(\"Individual\", set, fitness=creator.Fitness)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: (1, 20), 1: (2, 15), 2: (4, 10), 3: (6, 20), 4: (8, 10)}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IND_INIT_SIZE = 5\n",
    "MAX_ITEM = 3\n",
    "MAX_WEIGHT = 50\n",
    "NBR_ITEMS = 5\n",
    "\n",
    "random.seed(64)\n",
    "\n",
    "\n",
    "# 物品集合，每一种物品都有价值和重量\n",
    "items = {\n",
    "    0:(1,20),\n",
    "    1:(2,15),\n",
    "    2:(4,10),\n",
    "    3:(6,20),\n",
    "    4:(8,10)\n",
    "}\n",
    "# for i in range(NBR_ITEMS):\n",
    "#     items[i]=(random.randint(1,10),random.uniform(0,100))\n",
    "    \n",
    "items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{0, 3, 4}, {0, 1, 2}, {0, 1, 3}, {0, 1, 3}, {0, 2, 4}]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"attr_item\", random.randrange, NBR_ITEMS)\n",
    "# print(toolbox.attr_bool())\n",
    "\n",
    "toolbox.register(\"individual\", tools.initRepeat, creator.Individual,\n",
    "                 toolbox.attr_item, n=IND_INIT_SIZE)\n",
    "# toolbox.individual()\n",
    "\n",
    "\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.individual)\n",
    "toolbox.population(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evalKnapsack(individual):\n",
    "    weight = 0.0\n",
    "    value = 0.0\n",
    "    for item in individual:\n",
    "        weight += items[item][0]\n",
    "        value += items[item][1]\n",
    "    if len(individual) > MAX_ITEM or weight > MAX_WEIGHT:\n",
    "        return 10000, 0             # Ensure overweighted bags are dominated\n",
    "    return weight, value\n",
    "\n",
    "def cxSet(ind1, ind2):\n",
    "    \"\"\"Apply a crossover operation on input sets. The first child is the\n",
    "    intersection of the two sets, the second child is the difference of the\n",
    "    two sets.\n",
    "    \"\"\"\n",
    "    temp = set(ind1)                # Used in order to keep type\n",
    "    ind1 &= ind2                    # Intersection (inplace)\n",
    "    ind2 ^= temp                    # Symmetric Difference (inplace)\n",
    "    return ind1, ind2\n",
    "    \n",
    "def mutSet(individual):\n",
    "    \"\"\"Mutation that pops or add an element.\"\"\"\n",
    "    if random.random() < 0.5:\n",
    "        if len(individual) > 0:     # We cannot pop from an empty set\n",
    "            individual.remove(random.choice(sorted(tuple(individual))))\n",
    "    else:\n",
    "        individual.add(random.randrange(NBR_ITEMS))\n",
    "    return individual,\n",
    "\n",
    "toolbox.register(\"evaluate\", evalKnapsack)\n",
    "toolbox.register(\"mate\", cxSet)\n",
    "toolbox.register(\"mutate\", mutSet)\n",
    "toolbox.register(\"select\", tools.selNSGA2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gen\tnevals\tavg              \tstd                          \tmin    \tmax            \n",
      "0  \t20    \t[2507.9    32.75]\t[4325.5672102    20.40067401]\t[6. 0.]\t[10000.    55.]\n",
      "1  \t9     \t[ 8.45 41.25]    \t[ 2.74727137 11.38804197]    \t[ 3. 10.]\t[15. 55.]      \n",
      "2  \t10    \t[ 5.85 35.  ]    \t[ 3.91503512 18.09696107]    \t[0. 0.]  \t[15. 55.]      \n",
      "3  \t9     \t[ 4.45 32.  ]    \t[ 3.36860505 20.08730943]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "4  \t6     \t[ 3.6 25.5]      \t[ 3.7469988  22.85278976]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "5  \t7     \t[ 2.95 22.  ]    \t[ 3.66708331 22.49444376]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "6  \t9     \t[ 2.95 22.  ]    \t[ 3.66708331 22.49444376]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "7  \t8     \t[ 3.85 27.5 ]    \t[ 3.92778564 23.15707235]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "8  \t8     \t[ 4.5  29.25]    \t[ 4.18927201 25.26237321]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "9  \t9     \t[ 5. 33.]        \t[ 4.11096096 24.        ]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "10 \t10    \t[ 6.8 44. ]      \t[ 3.44383507 18.27566688]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "11 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "12 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "13 \t8     \t[ 6.8 44. ]      \t[ 3.44383507 18.27566688]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "14 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "15 \t9     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "16 \t7     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "17 \t9     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "18 \t10    \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "19 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "20 \t9     \t[ 6.05 40.25]    \t[ 3.72122292 20.21602087]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "21 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "22 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "23 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "24 \t8     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "25 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "26 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "27 \t7     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "28 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "29 \t7     \t[ 6.95 45.75]    \t[ 3.20117166 15.43332433]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "30 \t9     \t[ 7.25 46.75]    \t[ 3.09636884 15.35211712]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "31 \t9     \t[ 6.95 45.75]    \t[ 3.20117166 15.43332433]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "32 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "33 \t9     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "34 \t9     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "35 \t6     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "36 \t9     \t[ 6.6 43.5]      \t[ 3.54118624 18.37797595]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "37 \t10    \t[ 6.9 44.5]      \t[ 3.47706773 18.43230859]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "38 \t9     \t[ 6.6 43.5]      \t[ 3.54118624 18.37797595]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "39 \t9     \t[ 6.6 43.5]      \t[ 3.54118624 18.37797595]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "40 \t8     \t[ 6.6 43.5]      \t[ 3.54118624 18.37797595]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "41 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "42 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "43 \t6     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "44 \t9     \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "45 \t10    \t[ 6.5 43. ]      \t[ 3.5       18.1934054]      \t[0. 0.]  \t[ 9. 55.]      \n",
      "46 \t9     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "47 \t9     \t[ 6.8  44.25]    \t[ 3.26496554 17.41228015]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "48 \t8     \t[ 6.4 42.5]      \t[ 3.45543051 17.99305422]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "49 \t8     \t[ 6.8  44.25]    \t[ 3.26496554 17.41228015]    \t[0. 0.]  \t[ 9. 55.]      \n",
      "50 \t10    \t[ 6.8  44.25]    \t[ 3.26496554 17.41228015]    \t[0. 0.]  \t[ 9. 55.]      \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([{0, 1, 3},\n",
       "  Individual(),\n",
       "  {0},\n",
       "  {0, 1},\n",
       "  {0, 1},\n",
       "  Individual(),\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 2},\n",
       "  {0, 1, 2},\n",
       "  {0, 1, 2},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3},\n",
       "  {0, 1, 3}],\n",
       " [{'gen': 0,\n",
       "   'nevals': 20,\n",
       "   'avg': array([2507.9 ,   32.75]),\n",
       "   'std': array([4325.5672102 ,   20.40067401]),\n",
       "   'min': array([6., 0.]),\n",
       "   'max': array([10000.,    55.])},\n",
       "  {'gen': 1,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 8.45, 41.25]),\n",
       "   'std': array([ 2.74727137, 11.38804197]),\n",
       "   'min': array([ 3., 10.]),\n",
       "   'max': array([15., 55.])},\n",
       "  {'gen': 2,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 5.85, 35.  ]),\n",
       "   'std': array([ 3.91503512, 18.09696107]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([15., 55.])},\n",
       "  {'gen': 3,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 4.45, 32.  ]),\n",
       "   'std': array([ 3.36860505, 20.08730943]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 4,\n",
       "   'nevals': 6,\n",
       "   'avg': array([ 3.6, 25.5]),\n",
       "   'std': array([ 3.7469988 , 22.85278976]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 5,\n",
       "   'nevals': 7,\n",
       "   'avg': array([ 2.95, 22.  ]),\n",
       "   'std': array([ 3.66708331, 22.49444376]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 6,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 2.95, 22.  ]),\n",
       "   'std': array([ 3.66708331, 22.49444376]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 7,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 3.85, 27.5 ]),\n",
       "   'std': array([ 3.92778564, 23.15707235]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 8,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 4.5 , 29.25]),\n",
       "   'std': array([ 4.18927201, 25.26237321]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 9,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 5., 33.]),\n",
       "   'std': array([ 4.11096096, 24.        ]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 10,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.8, 44. ]),\n",
       "   'std': array([ 3.44383507, 18.27566688]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 11,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 12,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 13,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 6.8, 44. ]),\n",
       "   'std': array([ 3.44383507, 18.27566688]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 14,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 15,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 16,\n",
       "   'nevals': 7,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 17,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 18,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 19,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 20,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.05, 40.25]),\n",
       "   'std': array([ 3.72122292, 20.21602087]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 21,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 22,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 23,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 24,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 25,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 26,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 27,\n",
       "   'nevals': 7,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 28,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 29,\n",
       "   'nevals': 7,\n",
       "   'avg': array([ 6.95, 45.75]),\n",
       "   'std': array([ 3.20117166, 15.43332433]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 30,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 7.25, 46.75]),\n",
       "   'std': array([ 3.09636884, 15.35211712]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 31,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.95, 45.75]),\n",
       "   'std': array([ 3.20117166, 15.43332433]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 32,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 33,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 34,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 35,\n",
       "   'nevals': 6,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 36,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.6, 43.5]),\n",
       "   'std': array([ 3.54118624, 18.37797595]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 37,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.9, 44.5]),\n",
       "   'std': array([ 3.47706773, 18.43230859]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 38,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.6, 43.5]),\n",
       "   'std': array([ 3.54118624, 18.37797595]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 39,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.6, 43.5]),\n",
       "   'std': array([ 3.54118624, 18.37797595]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 40,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 6.6, 43.5]),\n",
       "   'std': array([ 3.54118624, 18.37797595]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 41,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 42,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 43,\n",
       "   'nevals': 6,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 44,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 45,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.5, 43. ]),\n",
       "   'std': array([ 3.5      , 18.1934054]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 46,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 47,\n",
       "   'nevals': 9,\n",
       "   'avg': array([ 6.8 , 44.25]),\n",
       "   'std': array([ 3.26496554, 17.41228015]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 48,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 6.4, 42.5]),\n",
       "   'std': array([ 3.45543051, 17.99305422]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 49,\n",
       "   'nevals': 8,\n",
       "   'avg': array([ 6.8 , 44.25]),\n",
       "   'std': array([ 3.26496554, 17.41228015]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])},\n",
       "  {'gen': 50,\n",
       "   'nevals': 10,\n",
       "   'avg': array([ 6.8 , 44.25]),\n",
       "   'std': array([ 3.26496554, 17.41228015]),\n",
       "   'min': array([0., 0.]),\n",
       "   'max': array([ 9., 55.])}])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.seed(64)\n",
    "NGEN = 50\n",
    "MU = 20\n",
    "LAMBDA = 10\n",
    "CXPB = 0.7\n",
    "MUTPB = 0.2\n",
    "\n",
    "pop = toolbox.population(n=MU)\n",
    "hof = tools.ParetoFront()\n",
    "stats = tools.Statistics(lambda ind: ind.fitness.values)\n",
    "stats.register(\"avg\", numpy.mean, axis=0)\n",
    "stats.register(\"std\", numpy.std, axis=0)\n",
    "stats.register(\"min\", numpy.min, axis=0)\n",
    "stats.register(\"max\", numpy.max, axis=0)\n",
    "\n",
    "algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats,\n",
    "                          halloffame=hof)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Individual({0, 1, 3}), Individual(), Individual({0}), Individual({0, 1}), Individual({0, 1}), Individual(), Individual({0, 1, 3}), Individual({0, 1, 2}), Individual({0, 1, 2}), Individual({0, 1, 2}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3}), Individual({0, 1, 3})]\n"
     ]
    }
   ],
   "source": [
    "print(pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<deap.tools.support.Statistics object at 0x000001B854EC6C50>\n"
     ]
    }
   ],
   "source": [
    "print(stats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Individual(), Individual({0}), Individual({0, 1}), Individual({0, 1, 2}), Individual({0, 1, 3})]\n"
     ]
    }
   ],
   "source": [
    "print(hof)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
